/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.console.ejb.business.custom;

import com.farmcode.api.runtime.FARMCodeContext;
import com.farmcode.base.dao.BaseFilter;
import com.farmcode.base.dao.DAOException;
import com.farmcode.base.manager.FacadeException;
import com.farmcode.console.api.business.BasePropertyManagerBean;
import com.farmcode.console.api.business.custom.ExtPropertyManager;
import com.farmcode.console.api.model.Project;
import com.farmcode.console.api.model.Property;
import com.farmcode.console.api.util.Constants;
import java.util.List;
import javax.ejb.Local;
import javax.ejb.Stateless;

/**
 *
 * @author Oscar Ivan Prado
 */
@Local(ExtPropertyManager.class)
@Stateless
public class ExtPropertyManagerBean extends BasePropertyManagerBean implements ExtPropertyManager{    
    

    @Override
    public List<Property> getProjectParameters(Project project) throws DAOException{
        return findPropertyByType(Constants.PARAMETER, project);
    }

    @Override
    public List<Property> getProjectVariables(Project project) throws DAOException{
        return findPropertyByType(Constants.VARIABLE, project);
    }
    
    private List<Property> findPropertyByType(String type, Project project) throws DAOException{
        propertyDAO.setFilter(new PropertyFilter(type, project));
        return propertyDAO.find();
    }
    
    @Override
    public Property getPropertyByName(Project project, String name) throws DAOException{
        propertyDAO.setFilter(new PropertyNameFilter(name, project));
        List<Property>  properties = propertyDAO.find();
        return properties.isEmpty() ? null : properties.get(0);
    }
    
    @Override
    public void createJDBCProperties(boolean jdbcManager, String plugin, String host, String driver, String schema, String user, String password, Project project) throws  FacadeException{
        
        if(plugin == null || plugin.isEmpty()){
            throw  new FacadeException("Plugin requerid");
        }
        
        if(jdbcManager){
            if(host == null || host.isEmpty()){
                throw  new FacadeException("Host requerid");
            }

            if(driver == null || driver.isEmpty()){
                throw  new FacadeException("Driver requerid");
            }
            
            if(user == null || user.isEmpty()){
                throw  new FacadeException("User requerid");
            }

            if(password == null || password.isEmpty()){
                throw  new FacadeException("Password requerid");
            }
            
            Property hostEntity = new Property();
            hostEntity.setType(Constants.PARAMETER);
            hostEntity.setName(FARMCodeContext.HOST);
            hostEntity.setProject(project);
            hostEntity.setValue(host);

            Property driverEntity = new Property();
            driverEntity.setName(FARMCodeContext.DRIVER);
            driverEntity.setType(Constants.PARAMETER);
            driverEntity.setValue(driver);
            driverEntity.setProject(project);
            
            Property schemaEntity = new Property();
            schemaEntity.setName(FARMCodeContext.SCHEMA);
            schemaEntity.setType(Constants.PARAMETER);
            schemaEntity.setValue(schema);
            schemaEntity.setProject(project);

            Property userEntity = new Property();
            userEntity.setName(FARMCodeContext.USER);
            userEntity.setType(Constants.PARAMETER);
            userEntity.setValue(user);
            userEntity.setProject(project);

            Property passwordEntity = new Property();
            passwordEntity.setName(FARMCodeContext.PASSWORD);
            passwordEntity.setType(Constants.PARAMETER);
            passwordEntity.setValue(password);
            passwordEntity.setProject(project);

            create(hostEntity);
            create(schemaEntity);
            create(driverEntity);
            create(userEntity);
            create(passwordEntity);
            
        }        
        
        Property pluginEntity = new Property();
        pluginEntity.setType(Constants.PARAMETER);
        pluginEntity.setName(FARMCodeContext.OBJECT_INFO_PLUGIN_MANAGER);
        pluginEntity.setValue(plugin);
        pluginEntity.setProject(project);
        
        create(pluginEntity);
        
    }

    class PropertyFilter extends BaseFilter{

        private String type;
        private Project project;

        public PropertyFilter(String type, Project project) {
            this.type = type;
            this.project = project;
        }
        
        @Override
        public void fill() {
            setParameter("type", "type =", type);
            setParameter("project", "property.project =", project);
        }
        
    }
    
    class PropertyNameFilter extends BaseFilter{

        private String name;
        private Project project;

        public PropertyNameFilter(String name, Project project) {
            this.name = name;
            this.project = project;
        }
        
        @Override
        public void fill() {
            setParameter("name", "property.name =", name);
            setParameter("project", "property.project =", project);
        }
        
    }
    
}
